﻿@model ListBoxModel
@using NewLife;
@using NewLife.Cube
@using XCode;
@using XCode.Configuration;
@using System.Collections;
@{
    var name = Model.Name;
    var value = Model.Value;
    var type = value.GetType();

    var atts = HtmlHelper.AnonymousObjectToHtmlAttributes(Model.HtmlAttributes);
    if (NewLife.Cube.CubeSetting.Current.BootstrapSelect)
        atts.Add("class", "multiselect");
    else
        atts.Add("class", "form-control");

    // 处理自动回发
    if (Model.AutoPostback) atts.Add("onchange", "$(this).parents('form').submit();");

    var optionLabel = Model.OptionLabel;

    var selectedValue = Model.SelectedValues;
}
<el-select v-model="modelForm.@name" placeholder="请选择" size="mini" style="width:100px;">

    @if (value is IEnumerable<SelectListItem> selectList)
    {
        //@Html.DropDownList(name, selectList, atts)

        foreach (var item in selectList)
        {
            <el-option key="@item.Value" label="@item.Value" value="@item.Value"></el-option>
        }
    }
    else if (type is IEnumerable data && type.GetElementTypeEx().As(typeof(IEntity)))
    {
        IEntityFactory factory = null;
        var dic = new Dictionary<String, String>();
        foreach (IEntity entity in data)
        {
            if (factory == null) factory = entity.GetType().AsFactory();

            var key = entity[factory.Unique.Name] + "";
            dic[key] = entity + "";
        }
        @Html.DropDownList(name, new SelectList(dic, "Key", "Value", selectedValue), optionLabel, atts)
    }
    else if (value is IDictionary dic)
    {
        foreach (var key in dic.Keys)
        {
            <el-option key="@key" label="@dic[key]" value="@key"></el-option>
        }

        //@Html.DropDownList(name, new SelectList(dic, "Key", "Value", selectedValue), optionLabel, atts)
    }
    else if (value is IEnumerable items)
    {
        @Html.DropDownList(name, new SelectList(items, selectedValue), optionLabel, atts)
    }
</el-select>
